#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution 
{
    TreeNode * createNode(int val)
    {
        return new TreeNode(val);
    }
    TreeNode * recursion(vector<int> & nums)
    {
        if(nums.empty())
        {
            return nullptr;
        }
        int maxIndex = 0;
        int maxValue = nums[0];
        for(int i = 0; i < nums.size(); i++)
        {
            if(nums[i] > maxValue)
            {
                maxIndex = i;
                maxValue = nums[i];
            }
        }
        auto mid = createNode(maxValue);
        vector<int> numsLeft;
        vector<int> numsRight;
        for(int i = 0; i < nums.size(); i++)
        {
            if(i < maxIndex)
            {
                numsLeft.push_back(nums[i]);
            }
            else if(i > maxIndex)
            {
                numsRight.push_back(nums[i]);
            }
        }
        mid->left = recursion(numsLeft);
        mid->right = recursion(numsRight);
        return mid;
    }

public:
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) 
    {
        auto root = recursion(nums);
        return root;
    }
};

// 辅助函数：释放二叉树内存
void deleteTree(TreeNode* root) {
    if (root == nullptr) return;
    deleteTree(root->left);
    deleteTree(root->right);
    delete root;
}

// 辅助函数：前序遍历打印树结构（用于验证）
void preorderPrint(TreeNode* root, vector<int>& result) {
    if (root == nullptr) {
        result.push_back(-1); // 用-1表示null
        return;
    }
    result.push_back(root->val);
    preorderPrint(root->left, result);
    preorderPrint(root->right, result);
}

// 辅助函数：中序遍历打印树结构（用于验证）
void inorderPrint(TreeNode* root, vector<int>& result) {
    if (root == nullptr) return;
    inorderPrint(root->left, result);
    result.push_back(root->val);
    inorderPrint(root->right, result);
}

// 辅助函数：层序遍历打印树结构（用于验证）
void levelOrderPrint(TreeNode* root, vector<int>& result) {
    if (root == nullptr) return;
    queue<TreeNode*> q;
    q.push(root);
    while (!q.empty()) {
        TreeNode* node = q.front();
        q.pop();
        if (node == nullptr) {
            result.push_back(-1);
        } else {
            result.push_back(node->val);
            q.push(node->left);
            q.push(node->right);
        }
    }
    // 移除末尾的-1
    while (!result.empty() && result.back() == -1) {
        result.pop_back();
    }
}

// 辅助函数：打印向量
void printVector(const vector<int>& vec, const string& name) {
    cout << name << ": [";
    for (int i = 0; i < vec.size(); i++) {
        if (vec[i] == -1) cout << "null";
        else cout << vec[i];
        if (i < vec.size() - 1) cout << ", ";
    }
    cout << "]" << endl;
}

int main() {
    Solution solution;
    
    cout << "=== LeetCode 654. 最大二叉树 测试案例 ===" << endl;
    
    // 测试案例1: 示例1 - [3,2,1,6,0,5]
    cout << "\n测试案例1: [3,2,1,6,0,5]" << endl;
    vector<int> nums1 = {3, 2, 1, 6, 0, 5};
    
    TreeNode* root1 = solution.constructMaximumBinaryTree(nums1);
    
    vector<int> preResult1, inResult1, levelResult1;
    preorderPrint(root1, preResult1);
    inorderPrint(root1, inResult1);
    levelOrderPrint(root1, levelResult1);
    
    cout << "输入: [3,2,1,6,0,5]" << endl;
    printVector(preResult1, "前序遍历结果");
    printVector(inResult1, "中序遍历结果");
    printVector(levelResult1, "层序遍历结果");
    cout << "期望: 根节点为6，左子树最大值为3，右子树最大值为5" << endl;
    
    // 测试案例2: 示例2 - [3,2,1]
    cout << "\n测试案例2: [3,2,1]" << endl;
    vector<int> nums2 = {3, 2, 1};
    
    TreeNode* root2 = solution.constructMaximumBinaryTree(nums2);
    
    vector<int> preResult2, inResult2, levelResult2;
    preorderPrint(root2, preResult2);
    inorderPrint(root2, inResult2);
    levelOrderPrint(root2, levelResult2);
    
    cout << "输入: [3,2,1]" << endl;
    printVector(preResult2, "前序遍历结果");
    printVector(inResult2, "中序遍历结果");
    printVector(levelResult2, "层序遍历结果");
    cout << "期望: 根节点为3，只有右子树" << endl;
    
    // 测试案例3: 只有一个元素 - [1]
    cout << "\n测试案例3: [1]" << endl;
    vector<int> nums3 = {1};
    
    TreeNode* root3 = solution.constructMaximumBinaryTree(nums3);
    
    vector<int> preResult3, inResult3, levelResult3;
    preorderPrint(root3, preResult3);
    inorderPrint(root3, inResult3);
    levelOrderPrint(root3, levelResult3);
    
    cout << "输入: [1]" << endl;
    printVector(preResult3, "前序遍历结果");
    printVector(inResult3, "中序遍历结果");
    printVector(levelResult3, "层序遍历结果");
    cout << "期望: 只有根节点1" << endl;
    
    // 测试案例4: 递增序列 - [1,2,3,4,5]
    cout << "\n测试案例4: [1,2,3,4,5]" << endl;
    vector<int> nums4 = {1, 2, 3, 4, 5};
    
    TreeNode* root4 = solution.constructMaximumBinaryTree(nums4);
    
    vector<int> preResult4, inResult4, levelResult4;
    preorderPrint(root4, preResult4);
    inorderPrint(root4, inResult4);
    levelOrderPrint(root4, levelResult4);
    
    cout << "输入: [1,2,3,4,5]" << endl;
    printVector(preResult4, "前序遍历结果");
    printVector(inResult4, "中序遍历结果");
    printVector(levelResult4, "层序遍历结果");
    cout << "期望: 根节点为5，只有左子树，形成右偏树" << endl;
    
    // 测试案例5: 递减序列 - [5,4,3,2,1]
    cout << "\n测试案例5: [5,4,3,2,1]" << endl;
    vector<int> nums5 = {5, 4, 3, 2, 1};
    
    TreeNode* root5 = solution.constructMaximumBinaryTree(nums5);
    
    vector<int> preResult5, inResult5, levelResult5;
    preorderPrint(root5, preResult5);
    inorderPrint(root5, inResult5);
    levelOrderPrint(root5, levelResult5);
    
    cout << "输入: [5,4,3,2,1]" << endl;
    printVector(preResult5, "前序遍历结果");
    printVector(inResult5, "中序遍历结果");
    printVector(levelResult5, "层序遍历结果");
    cout << "期望: 根节点为5，只有右子树，形成左偏树" << endl;
    
    // 测试案例6: 复杂情况 - [4,1,3,null,null,2]对应的数组 [4,1,3,2]
    cout << "\n测试案例6: [4,1,3,2]" << endl;
    vector<int> nums6 = {4, 1, 3, 2};
    
    TreeNode* root6 = solution.constructMaximumBinaryTree(nums6);
    
    vector<int> preResult6, inResult6, levelResult6;
    preorderPrint(root6, preResult6);
    inorderPrint(root6, inResult6);
    levelOrderPrint(root6, levelResult6);
    
    cout << "输入: [4,1,3,2]" << endl;
    printVector(preResult6, "前序遍历结果");
    printVector(inResult6, "中序遍历结果");
    printVector(levelResult6, "层序遍历结果");
    cout << "期望: 根节点为4，左子树为1，右子树根节点为3" << endl;
    
    // 测试案例7: 山峰形状 - [1,3,5,4,2]
    cout << "\n测试案例7: [1,3,5,4,2]" << endl;
    vector<int> nums7 = {1, 3, 5, 4, 2};
    
    TreeNode* root7 = solution.constructMaximumBinaryTree(nums7);
    
    vector<int> preResult7, inResult7, levelResult7;
    preorderPrint(root7, preResult7);
    inorderPrint(root7, inResult7);
    levelOrderPrint(root7, levelResult7);
    
    cout << "输入: [1,3,5,4,2]" << endl;
    printVector(preResult7, "前序遍历结果");
    printVector(inResult7, "中序遍历结果");
    printVector(levelResult7, "层序遍历结果");
    cout << "期望: 根节点为5，左右子树都存在" << endl;
    
    // 释放内存
    deleteTree(root1);
    deleteTree(root2);
    deleteTree(root3);
    deleteTree(root4);
    deleteTree(root5);
    deleteTree(root6);
    deleteTree(root7);
    
    cout << "\n=== 测试完成 ===" << endl;
    cout << "\n注意：如果结果不正确，可能是因为：" << endl;
    cout << "第50行的条件应该是 'else if(i > maxIndex)' 而不是 'else'" << endl;
    cout << "最大值本身不应该被包含在左右子数组中" << endl;
    
    return 0;
}